package com.jiazhong.shopping.servlets;

import com.jiazhong.shopping.dao.CarDao;
import com.jiazhong.shopping.dao.impl.CarDaoImpl;
import com.jiazhong.shopping.model.Car;
import com.jiazhong.shopping.model.CarInfo;
import com.jiazhong.shopping.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/car/car.do")
public class CarServlet extends BaseServlet {
    private CarDao carDao = new CarDaoImpl();
    /**
     * 添加购物车
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void addCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得客户端提交的食品编号
        int eat_id = Integer.parseInt(request.getParameter("eat_id"));
        //获得客户端提交的购买数量
        int count = Integer.parseInt(request.getParameter("count"));
        //从session中获得User对象并取得userId
        User user = (User) request.getSession().getAttribute("user");

        Car car = new Car();
        car.setEat_id(eat_id);
        car.setCar_count(count);
        car.setUserId(user.getUser_id());
        /***
         * 实现思路:
         *  1.判断要添加的菜品在当前用户的购物车中是否存在
         *  2.如果存在则购买数量增加
         *  3.如果不存在向购物车中新增菜品
         */
        //1.判断要添加的菜品在当前用户的购物车中是否存在
        boolean boo = carDao.exists(eat_id,user.getUser_id());
        if(boo){//存在
            //2.如果存在则购买数量增加
            carDao.updateCarCount(car);
        }else{//不存在
            //3.如果不存在向购物车中新增商品
            carDao.addCar(car);
        }
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("<script>alert('购物车添加成功');location.href='../index.jsp';</script>");


       /* 版本1
       //获得客户端提交的菜品编号
        int eat_id = Integer.parseInt(request.getParameter("eat_id"));
        //获得客户端提交的购买数量
        int count = Integer.parseInt(request.getParameter("count"));

        //创建Car对象，并将获得的数据封装到car对象中
        Car car = new Car();
        car.setEat_id(eat_id);
        car.setCar_count(count);

        //从session中获得User对象并取得userId
        User user = (User) request.getSession().getAttribute("user");
        car.setUserId(user.getUser_id());
        //调用底层处理类进行功能处理
        carDao.addCar(car);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("<script>alert('购物车添加成功');location.href='../index.jsp';</script>");*/
    }

    /**
     * 获得购物车数据
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void showCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得当前登录者的编号
        int userId = ((User)(request.getSession().getAttribute("user"))).getUser_id();
        System.out.println(userId);
        //查询当前登录者的购物车信息
        List<CarInfo> carInfoList = carDao.queryCarInfo(userId);
        //将获得的购物车数据存入到request对象中
        request.setAttribute("carInfoList",carInfoList);
        //请求转发到showCar.jsp
        request.getRequestDispatcher("showCar.jsp").forward(request,response);
        //response.sendRedirect("/car/bg/showCar.jsp");

    }

    /**
     * 从购物车中删除一个商品
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void delCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得客户端提交的数据
        int car_id = Integer.parseInt(request.getParameter("car_id"));
        //调用底层处理类进行功能处理
        carDao.delCar(car_id);
        //请求转发到showCarServlet中
        request.getRequestDispatcher("car.do?operate=showCar").forward(request,response);
        //response.sendRedirect("car.do?operate=showCar");
    }


    /**
     * 更新购物车中商品的数量
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void updateCarCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收客户端传入的参数
        int car_id = Integer.parseInt(request.getParameter("car_id"));
        int count = Integer.parseInt(request.getParameter("count"));
        //调用底层处理类进行功能处理
        carDao.updateCarCount(car_id,count);
        //调用showCar Servlet方法
        this.showCar(request,response);

    }
}
